package com.huizu.project.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.dx.SecurityUtil;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.ProLd;
import com.huizu.project.service.IProLdService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

/**
 * 宿舍/教/实验劳动Controller
 *
 * @author zhangmx
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/project/prold")
public class ProLdController extends BaseController
{
    @Autowired
    private IProLdService proLdService;
    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询宿舍/教/实验劳动列表
     */
    @PreAuthorize("@ss.hasPermi('project:prold:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProLd proLd) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())){
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proLd.setBjIds(bjs);
        }
        startPage();
        List<ProLd> list = proLdService.selectProLdList(proLd);
        return getDataTable(list);
    }

    /**
     * 导出宿舍/教/实验劳动列表
     */
    @PreAuthorize("@ss.hasPermi('project:prold:export')")
    @Log(title = "基础劳动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProLd proLd)
    {
        List<ProLd> list = proLdService.selectProLdList(proLd);
        ExcelUtil<ProLd> util = new ExcelUtil<ProLd>(ProLd.class);
        util.exportExcel(response, list, "宿舍/教/实验劳动数据");
    }

    /**
     * 获取宿舍/教/实验劳动详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:prold:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(proLdService.selectProLdById(id));
    }

    /**
     * 新增宿舍/教/实验劳动
     */
    @PreAuthorize("@ss.hasPermi('project:prold:add')")
    @Log(title = "基础劳动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProLd proLd)
    {
        return toAjax(proLdService.insertProLd(proLd));
    }

    /**
     * 修改宿舍/教/实验劳动
     */
    @PreAuthorize("@ss.hasPermi('project:prold:edit')")
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProLd proLd)
    {
        return toAjax(proLdService.updateProLd(proLd));
    }

    /**
     * 删除宿舍/教/实验劳动
     */
    @PreAuthorize("@ss.hasPermi('project:prold:remove')")
    @Log(title = "基础劳动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proLdService.deleteProLdByIds(ids));
    }

    /**
     * 通过
     */
    @PreAuthorize("@ss.hasPermi('project:prold:edit')")
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PutMapping("/pass")
    public AjaxResult pass(@RequestBody List<ProLd> proLds)
    {
        for (ProLd proLd : proLds) {
            proLdService.pass(proLd);
        }
        return toAjax(1);
    }

    /**
     * 驳回
     */
    @PreAuthorize("@ss.hasPermi('project:prold:edit')")
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody List<ProLd> proLds)
    {
        for (ProLd proLd : proLds) {
            proLdService.reject(proLd);
        }
        return toAjax(1);
    }

    /**
     * 通过后打回
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('project:prold:repulse')")
    @Log(title = "基础劳动", businessType = BusinessType.UPDATE)
    @PutMapping("/repulse")
    public AjaxResult repulse(@RequestBody ProLd proLd) {
        return toAjax(proLdService.repulse(proLd));
    }
}
